package com.woniu.cloud.storeassembly.apiimpl;

import com.woniu.cloud.api.StoreApi;
import com.woniu.cloud.common.dto.Result;
import com.woniu.cloud.form.StoreInsertForm;
import com.woniu.cloud.form.StoreUpdateForm;
import com.woniu.cloud.entity.TStore;
import com.woniu.cloud.storeassembly.service.ITStoreService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.*;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 平台对店铺商品表 前端控制器
 * </p>
 *
 * @author hyy
 * @since 2021-09-22
 */
@RestController
@Slf4j
public class StoreApiImpl implements StoreApi {



    @Resource
    private ITStoreService storeService;


    @Resource
    private RedissonClient redissonClient;

    @GetMapping("test")
    public void redis(Integer storeId) {
        RGeo<Object> geo = redissonClient.getGeo("near");
        List<Object> cities = geo.radius(15, 37, 200, GeoUnit.KILOMETERS);
        Map<Object, GeoPosition> citiesWithPositions = geo.radiusWithPosition(1, 1, 2000, GeoUnit.KILOMETERS);
        for (Map.Entry<Object, GeoPosition> entry : citiesWithPositions.entrySet()) {
        GeoPosition mapValue = entry.getValue();
        System.out.println(entry.getKey() + "：" + mapValue.toString());
        }
    }

    @Override
    @GetMapping("getStoreByStoreId")
    @GlobalTransactional
    public Result getStoreByStoreId(Integer storeId) {
        log.info("通过Id查找店铺->storeId:{}",storeId);
        if (null==storeId) {
            return Result.fail("参数异常",null);
        }
        try {
            log.info("开始查询->storeId:{}",storeId);
            return storeService.getStoreByStoreId(storeId);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("服务异常",null);
        }
    }

    @Override
    @GetMapping("getStoreList")
    @GlobalTransactional
    public Result getStoreList(Integer serviceId, Double storeLongitude, Double storeLatitude,Double radius) {
        log.info("附近店铺查找->storeLongitude:{},storeLatitude:{}",storeLongitude,storeLatitude);
        if (null==storeLongitude||null==storeLatitude) {
            return Result.fail("参数异常",null);
        }
        try {
            log.info("开始查询->storeLongitude:{},storeLatitude:{}",storeLongitude,storeLatitude);

            return storeService.getStoreList(serviceId,storeLongitude,storeLatitude,null==radius?2:radius);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("服务异常",null);
        }


    }

    @Override
    @PostMapping("insertStore")
    @GlobalTransactional
    public Result insertStore(@RequestBody StoreInsertForm storeForm) {
        log.info("添加店铺->storeForm:{}",storeForm);
        if (null==storeForm) {
            return Result.fail("参数异常",null);
        }
        try {
            TStore store = new TStore();
            BeanUtils.copyProperties(storeForm,store);
            log.info("开始添加->store:{}",store);
            return storeService.insertStore(store);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("服务异常",null);
        }
    }

    @Override
    @PutMapping("updateStore")
    @GlobalTransactional
    public Result updateStore(@RequestBody StoreUpdateForm storeForm) {
        log.info("修改店铺->storeForm:{}",storeForm);
        if (null==storeForm) {
            return Result.fail("参数异常",null);
        }
        try {
            log.info("开始修改->storeForm:{}",storeForm);
            TStore store = new TStore();
            BeanUtils.copyProperties(storeForm,store);
            return storeService.updateStore(store);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("服务异常",null);
        }
    }

    @Override
    public Result insertStorePay(Integer storeId, BigDecimal payNum) {
        log.info("->storeId:{},payNum:{}",storeId,payNum);
        if (null==storeId&&null==payNum) {
            return Result.fail("参数异常",null);
        }
        try {
            log.info("账户修改->storeId:{},payNum:{}",storeId,payNum);
            return storeService.insertStorePay(storeId,payNum);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("服务异常",null);
        }

    }

}
